package com.jxb.test20241216;

import java.util.Collections;
import java.util.LinkedList;
import java.util.Stack;

/**
 * 类注释
 *
 * @author jiaobo
 * @date Created in 2024/12/22 21:25
 **/
public class DecodeString_394_UseStack {

    public static void main(String[] args) {
        String s = "2[leetcode]";
        String s1 = decodeStringList(s);
        System.out.println(s1);
    }

    static int index = 0;

    //
    public static String decodeStringStack(String s) {
        //返回结果
        String res = "";
        Stack<String> resStack = new Stack<>();
        Stack<Integer> intStack = new Stack<>();
        while (index < s.length()) {
            char c = s.charAt(index);
            if (Character.isDigit(c)) {
                //若是数字的话，获取完整的数字进行入栈
                Integer totalNum = getTotalNum(s);
                intStack.push(totalNum);
            }
            else if (c == '[') {
                resStack.push(res);
                res = "";
                index++;
            }else if (c == ']') {
                //取出数字
                Integer number = intStack.pop();
                StringBuilder sb = new StringBuilder();
                while (number > 0) {
                    String pop = resStack.pop();
                    sb.append(pop);
                    number--;
                }
                res  = resStack.pop() + sb;
                index++;
            }else {
                //处理普通字符
                res = res + c;
                index++;
            }
        }
        return res;
    }


    public static String decodeStringList(String s) {
        LinkedList<String> list = new LinkedList<>();
        while (index < s.length()) {
            char c = s.charAt(index);
            if (Character.isDigit(c)) {
                //获取完整的数字
                Integer totalNum = getTotalNum(s);
                list.addLast(String.valueOf(totalNum));
            }else if (c == '[' || Character.isLetter(c)) {
                list.addLast(String.valueOf(c));
                index++;
            }else {
                //获取字符
                LinkedList<String> sub = new LinkedList<String>();
                while (!"[".equals(list.peekLast())) {
                    sub.addLast(list.removeLast());
                }
                //字符反转
                Collections.reverse(sub);

                //获取数字
                list.removeLast();
                Integer number = Integer.valueOf(list.removeLast());
                //拼接字符串
                StringBuilder sb = new StringBuilder();
                for (String str:sub) {
                    sb.append(str);
                }
                StringBuilder stringBuilder = new StringBuilder();
                while (number > 0) {
                    stringBuilder.append(sb);
                    number--;
                }
                if (list.peekLast() != null ) {
                    list.addLast(list.removeLast() + stringBuilder);
                }else {
                    list.addLast(stringBuilder.toString());
                }
                index++;
            }
        }
        //拼接字符
        StringBuilder res = new StringBuilder();
        for (String str:list) {
            res.append(str);
        }
        return res.toString();
    }

    private static Integer getTotalNum(String s) {
        char c = s.charAt(index);
        StringBuilder sb = new StringBuilder();
        while (Character.isDigit(c)) {
            sb.append(c);
            index++;
            c = s.charAt(index);
        }
        return Integer.valueOf(sb.toString());
    }

}
